import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import officeRouter from './modules/office'
import financeRouter from './modules/finance'
import calculatorRouter from './modules/calculator'
import casesRouter from './modules/cases'
import gaokaoRouter from './modules/gaokao'
import wechatRouter from './modules/wechat'
import documentRouter from './modules/document'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  },

  {
    path: '/users',
    component: Layout,
    redirect: '/users/users_list',
    name: 'Users',
    meta: { title: '用户管理' },
    children: [
      {
        path: 'users_list',
        name: 'users-list',
        component: () => import('@/views/users/index'),
        meta: { title: '用户信息' }
      },
    ]
  },

  {
    path: '/permissions',
    component: Layout,
    redirect: '/permissions/perms_list',
    name: 'permissions',
    meta: { title: '权限管理' },
    children: [
      {
        path: 'perms_list',
        component: () => import('@/views/permissions/permissions'), // Parent router-view
        name: 'perms-list',
        meta: { title: '权限信息' },
      },
      {
        path: 'roles_list',
        component: () => import('@/views/permissions/roles'),
        name: 'roles-list',
        meta: { title: '角色信息' }
      }
    ]
  },
  officeRouter,
  financeRouter,
  calculatorRouter,
  casesRouter,
  gaokaoRouter,
  wechatRouter,
  documentRouter,
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
